/*
 * @Author: 张久冬
 * @Date: 2021-12-15 11:29:37
 * @
# Description:
 */
import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import LocalCache from '@/utils/cache'
import { firstMenu } from '@/utils/map-menus'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/main/'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue')
  },
  {
    path: '/main',
    name: 'main',
    component: () => import('@/views/main/main.vue')
    // children: [] //根据用户的权限确定
  },
  {
    path: '/:pathMatch(.*)*',
    name: '404',
    component: () => import('@/views/404notFound/index.vue')
  }
]
const router = createRouter({
  routes,
  history: createWebHashHistory()
})
// 导航守卫
router.beforeEach((to) => {
  if (to.path !== '/login') {
    const token = LocalCache.getCache('token')
    if (!token) {
      return '/login'
    }
  }
  if (to.path === '/main/') {
    return firstMenu.url
  }
})
export default router
